﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheControllerRisk.Settings;
using System.Reflection;

namespace RiskMan.DataClass.AlfaDirectFolder
{
    public class TransactionAlfaDirect
    {
        MainWindow mainWindow;
        private int len;
        Random r = new Random();



        public TransactionAlfaDirect(MainWindow mainWindow)
        {
            len = r.Next(10000, 999999);
            this.mainWindow = mainWindow;
            //mainWindow._dataProvider.AD
        }

        /// <summary>
        /// выставление лимитированной заявки
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="PlaceCode"></param>
        /// <param name="PCode"></param>
        /// <param name="Operation"></param>
        /// <param name="Quantity"></param>
        /// <param name="Price"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public long LimitOrder(string Account, string PlaceCode, string PCode, string Operation, int Quantity, double Price)
        {
            len++;
            try
            {
                int ordNo = mainWindow._dataProvider.AD.CreateLimitOrder(
                    Account, PlaceCode, PCode, DateTime.Today.AddDays(1),
                    len.ToString(), "RUR", Operation, Math.Abs(Quantity), Price,
                    null, null, null, null, null, null, null, null,
                    null, null, null, null, null, null, null, null, -1);

                cfg.SetLog("Заявка c комментарием: " + len + "\r\n" + mainWindow._dataProvider.AD.LastResultMsg, MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
                return len;

            }
            catch (Exception ex)
            {
                cfg.SetLog(mainWindow._dataProvider.AD.LastResultMsg, MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
            return 0;
        }

        /// <summary>
        /// Снятие заявки
        /// </summary>
        /// <param name="number"></param>
        public void DropOrder(long number)
        {
            try
            {
                mainWindow._dataProvider.AD.DropOrder(number, "BS", null, null, null, null, -1);
                cfg.SetLog("Заявка №" + number + "\r\n" + mainWindow._dataProvider.AD.LastResultMsg, MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
            catch
            {
                cfg.SetLog(" Снятие заявки " + "Ошибка" + "\r\n" + mainWindow._dataProvider.AD.LastResultMsg, MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
        }

        public long StopLimit(string Account, string PlaceCode, string PCode, string Operation, int Quantity, double Price, double otstup, double profit)
        {
            len++;
            try
            {
                int ordNo = mainWindow._dataProvider.AD.CreateStopOrder(
                    Account, PlaceCode, PCode, DateTime.Today.AddDays(1),
                    len.ToString(), "RUR", Operation, Math.Abs(Quantity), Price, otstup, profit, -1);

                cfg.SetLog("Стоп-заявка c комментарием: " + len + "\r\n" + mainWindow._dataProvider.AD.LastResultMsg, MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
                return len;

            }
            catch (Exception ex)
            {
                cfg.SetLog(mainWindow._dataProvider.AD.LastResultMsg, MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
            return 0;
        }
    }
}
